<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .item{
        width: 100px;
        height: 50px;
        border: solid 1px deepskyblue;
    }
</style>
<body>
    <div class="container">
        <h2>点击切换颜色</h2>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</body>
<script>
    let items=document.getElementsByClassName('item')
    console.log(items);

    for(let i =0;i<items.length;i++){
        items[i].onclick = function(){
            items[i].style.background = 'pink'
        }
    }
</script>
</html>